package com.example.mapper;

import com.example.info.LoginInfo;
import com.example.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * 查询所有userType为1的普通用户
     * 从user表中查询ID,userName、userType、realName、job
     */
    @Select("select userID, userName, userType, realName, job from user where userType = 1")
    List<User> findAll();


    /**
     * 添加普通用户
     */
    @Insert("insert into user (userName, userType, realName, job, create_time, update_time) values (#{userName},  #{userType}, #{realName}, #{job}, #{createTime}, #{updateTime})")
    void insert(User user);

    /**
     * 根据ID删除普通用户
     */
    @Delete("delete from user where userID = #{userID}")
    void deleteById(Integer userID);

    /**
     * 重置自增主键
     */
    @Update("alter table user auto_increment = 1")
    void resetAutoIncrement();

    /**
     * 更新ID大于指定值的记录的ID
     */
    @Update("update user set userID = userID - 1 where userID > #{userID}")
    void decrementUserIDAfter(Integer userID);

    /**
     * 根据ID查询普通用户
     */
    @Select("select userName, realName, job from user where userID = #{userID}")
    User getById(Integer userID);

    /**
     * 修改普通用户
     */
    @Update("update user set userName = #{userName}, realName = #{realName}, job = #{job}, update_time = #{updateTime} where userID = #{userID}")
    void update(User user);

    //获取所有用户名
    @Select("select userName from user")
    public List<String> getUserName();

    //根据用户名获得密码
    @Select("select userPassword from user where userName = #{userName}")
    public LoginInfo getPasswordByUserName(String userName);
}
